var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp()
Page({
  data: {
    keywrod: '',
    searchStatus: false,
    goodsList: [],
    helpKeyword: [],
    historyKeyword: [],
    categoryFilter: false,
    currentSortType: 'default',
    currentSortOrder: '',
    filterCategory: [],
    defaultKeyword: {},
    hotKeyword: [],
    page: 1,
    size: 20,
    currentSortType: 'id',
    currentSortOrder: 'desc',
    categoryId: 0
  },
  //事件处理函数
  closeSearch: function () {
    wx.navigateBack()
  },
  clearKeyword: function () {
    this.setData({
      keyword: '',
      searchStatus: false
    });
  },
  onLoad: function () {
    this.getSearchKeyword();
  },
  //初始化 注意这里搜索不能为空后台处理
  getSearchKeyword() {
    let that = this;  
    util.request(api.SearchIndex).then(function (res) {
      if (res.code === 0) {
        that.setData({
          historyKeyword: res.data.historyKeywordList,
          defaultKeyword: res.data.defaultKeyword,
          hotKeyword: res.data.hotKeywordList
        });
      }
    }); 
  },
  
  inputChange: function (e) {
    this.setData({
      keyword: e.detail.value,
      searchStatus: false
    });
    this.getHelpKeyword();
  },
  //搜索推荐商品
  getHelpKeyword: function () {
    let that = this; 
    if (that.data.keyword !=null){
      util.request(api.SearchHelper, { keyword: that.data.keyword }).then(function (res) {
        if (res.code === 0) {
          that.setData({
            helpKeyword: res.data
          });
        }
      });
    } 
  },
  inputFocus: function () {
    this.setData({
      searchStatus: false,
      goodsList: []
    });
    if (this.data.keyword) {
        this.getHelpKeyword();
    }
  },
  clearHistory: function () {
    this.setData({
      historyKeyword: []
    })
    util.request(api.SearchClearHistory, {}, 'POST')
      .then(function (res) {
       console.log('清除成功');
      });
  },
//2.根据搜索关键字 注意keyword在后台是(string字符串)
  getGoodsList: function () {
    let that = this;  
    util.request(api.GoodsList, { keyword: that.data.keyword, page: that.data.page, size: that.data.size, sort: that.data.currentSortType, order: that.data.currentSortOrder, categoryId: that.data.categoryId }).then(function (res) { 
      if (res.code === 0) {
        that.setData({
          searchStatus: true,
          categoryFilter: false,
          goodsList: res.data.goodsList,
          filterCategory: res.data.filterCategoryList,
          page: res.data.page,
          size: res.data.size
        });
      }
      //重新获取关键词
      that.getSearchKeyword();
    });
  },
  onKeywordTap: function (event) { 
    this.getSearchResult(event.target.dataset.keyword);

  },
  // 1.关键字
  getSearchResult(keyword) { 
    this.setData({
      keyword: keyword,
      page: 1,
      categoryId: 0,
      goodsList: []
    });
    this.getGoodsList();
  },
  //3.点击分类类目(根据关键字查询)
  openSortFilter: function (event) { 
    let currentId = event.currentTarget.id; 
    switch (currentId) {
      case 'categoryFilter': 
        this.setData({
          'categoryFilter': !this.data.categoryFilter,
          'currentSortOrder': 'asc'
        });
        break;
      case 'priceSort':
        let tmpSortOrder = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmpSortOrder = 'desc';
        }
        this.setData({
          'currentSortType': 'price',
          'currentSortOrder': tmpSortOrder,
          'categoryFilter': false
        }); 
        this.getGoodsList();
        break;
      default:
        //综合排序
        this.setData({
          'currentSortType': 'default',
          'currentSortOrder': 'desc',
          'categoryFilter': false
        });
        this.getGoodsList();
    }
  },

  //4.选中分类目录
  selectCategory: function (event) { 
    let currentIndex = event.target.dataset.categoryIndex; 
    let filterCategory = this.data.filterCategory; 
    let currentCategory = null;
    //判断选中的分类目录
    for (let key in filterCategory) {
      if (key == currentIndex) { 
        filterCategory[key].selected = true;
        currentCategory = filterCategory[key];
      } else { 
        filterCategory[key].selected = false;
      }
    }
    //6.用于查询(做为条件)选中分类 keyword关键字清空否则查询错误
    this.setData({
      filterCategory: filterCategory,
      categoryFilter: false,
      categoryId: currentCategory.id, 
      keyword:'',
      page: 1,
      goodsList: []
    });  
    this.getGoodsList();
  },
  onKeywordConfirm(event) {
    this.getSearchResult(event.detail.value);
  }
})